#include "aio_key.h"
#include "aio_callback_thread.h"
#include "../msg/head.h"
#include "aio_thread.h"
#include "aio_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    aio_key::aio_key()
    {
        memset( &m_remote_addr, 0, sizeof( m_remote_addr ) );
        memset( &m_local_addr, 0, sizeof( m_local_addr ) );
        m_eState = KEY_STATE_NONE;
        m_nEpollFlags = 0;
        m_nRequestNum = 0;
        m_bCanWrite = false;
        m_enumHandleType = H_TYPE_NONE;
        m_pHandle = 0;
        m_lpUserKey = 0;
        m_lpMsg = 0;
    }

    aio_key::~aio_key()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int aio_key::helper_finish_request( aio_impl * lpAioImpl, msg_impl::LIST & data_list )
    {
        int nRet = 0;

        for( msg_impl * p = data_list.begin(); p != data_list.end(); )
        {
            msg_impl * lpMsg = p;
            p = p->m_pNext;
            lpMsg->m_bData[0] = false;
            lpAioImpl->m_pCallbackThread->notify( lpMsg );
        }

        data_list.clear();

        return nRet;
    }

    int aio_key::clear_recv_list( msg_impl::LIST & recv_list )
    {
        int nRet = 0;

        for( msg_impl * p = recv_list.begin(); p != recv_list.end(); )
        {
            buf_impl * lpBuf = p->m_lpBuf[0];
            msg_impl * lpMsg = p;
            p = p->m_pNext;

            if( lpBuf )
            {
                lpBuf->release();
                lpBuf = 0;
            }

            lpMsg->release();
            lpMsg = 0;
        }

        recv_list.clear();

        return nRet;
    }

    int aio_key::finish_request( aio_impl * lpAioImpl )
    {
        int nRet = 0;
        
        helper_finish_request( lpAioImpl, m_pend_accept_list );
        helper_finish_request( lpAioImpl, m_pend_recv_list );
        helper_finish_request( lpAioImpl, m_pend_send_list );
        m_eState = KEY_STATE_CLOSED;

        return nRet;
    }

    int aio_key::init()
    {
        using namespace std;
        int nRet = 0;
        return nRet;
    }

    int aio_key::term()
    {
        int nRet = 0;

        m_pend_accept_list.put_back_to_pool();
        m_pend_recv_list.put_back_to_pool();
        m_pend_send_list.put_back_to_pool();
        m_accept_list.put_back_to_pool();
        clear_recv_list( m_recv_list );

        memset( &m_remote_addr, 0, sizeof( m_remote_addr ) );
        memset( &m_local_addr, 0, sizeof( m_local_addr ) );
        m_eState = KEY_STATE_NONE;
        m_nEpollFlags = 0;
        m_nRequestNum = 0;
        m_bCanWrite = false;
        m_enumHandleType = H_TYPE_NONE;
        m_lpUserKey = 0;
        m_lpMsg = 0;

        if( m_pHandle )
        {
            m_pHandle->release();
            m_pHandle = 0;
        }

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int aio_key::release()
    {
        using namespace std;
        int nRet = 0;

        term();
        put_back_lock();

        return nRet;
    }

} // xos
